 /**
 * Description: 部门控制器
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-11 下午 18:32:34
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-11   xk       0.0.1     Initial
 */
package com.metro.paperless.sys.controller;

 import com.metro.core.annotation.BusinessDesc;
 import com.metro.core.vo.JsonResult;
 import com.metro.core.vo.QueryParams;
 import com.metro.core.vo.ResultCode;
 import com.metro.core.vo.inf.IResult;
 import com.metro.paperless.common.beans.constant.DicTypeEnum;
 import com.metro.paperless.common.utils.DicUtil;
 import com.metro.paperless.sys.beans.vo.SysDeparment;
 import com.metro.paperless.sys.service.inf.ISysDeparmentService;
 import com.metro.paperless.web.controller.BaseController;
 import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.ModelAndView;

 import javax.servlet.http.HttpServletRequest;
 import java.util.List;

 /**
 * 部门控制器<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-11]
 */
@Controller
@RequestMapping("/sysdeparment")
public class SysDeparmentController extends BaseController {
    //菜单编码
    private static final String menuCode = "sysdeparment";
    //模块描述
    private static final String MODULE_DESC = "部门";
    @Autowired
    private ISysDeparmentService sysDeparmentService;
    @Override
    protected String getViewDir() {
        return "sys/";
    }

     /**
      * 去查询部门
      *
      * @param sysDeparment 部门页面表单对象
      * @param page 分页配置
      * @return 结果视图
      * @author xk
      * @since [0.0.1]
      * @version [0.0.1, 2018-01-11]
      */
     @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询部门列表")
     @RequiresPermissions(menuCode + ":view")
     @SuppressWarnings("all")
     @RequestMapping(value = "list", method = {RequestMethod.GET,RequestMethod.POST})
     public ModelAndView list(SysDeparment sysDeparment) {
         ModelAndView mav = new ModelAndView();
         try {
             mav.setViewName(getView("sysdeparment_list"));
         } catch (Exception ex) {
             log.error("查询部门出错", ex);
             mav.addObject("ex", ex);
             mav.setViewName("/commons/error/error");
         }
         return mav;
     }

     /**
      * 查询ztree列表
      */
     @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询部门列表")
     @RequiresPermissions(menuCode + ":view")
     @RequestMapping(value = "queryForTree", method = { RequestMethod.GET })
     @ResponseBody
     public JsonResult queryForTree() {
         JsonResult jr = new JsonResult();
         SysDeparment entity = null;
         QueryParams<SysDeparment> params = new QueryParams<>();
         params.setOrderColumn("inner_code");
         List<SysDeparment> list = sysDeparmentService.queryForList(params);
         jr.setData(list);
         jr.setResult(ResultCode.SUCCESS);
         return jr;
     }

    /**
     * 新增部门
     * 
     * @param entity 部门页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-11]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增部门")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveSysDeparment(SysDeparment entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = sysDeparmentService.save(entity);
            DicUtil.reloadDataInDicTypeEnumPar(DicTypeEnum.DEPARTMENT);
            jr.setResult(res);
            log.debug("新增部门成功【{}】", entity);
        } catch (Exception ex) {
            log.error("新增部门出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 删除部门
     * 
     * @param id 部门页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-11]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除部门")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable Long id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除部门id={}",id);
            IResult codeMsg = sysDeparmentService.delete(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除部门出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 修改部门
     * 
     * @param entity 部门页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-11]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改部门")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateSysDeparment(SysDeparment entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = sysDeparmentService.update(entity);
            jr.setResult(res);
            log.debug("修改部门成功【{}】", entity);
        } catch (Exception ex) {
            log.error("修改部门出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

     @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改部门")
     @RequiresPermissions(menuCode + ":edit")
     @RequestMapping(value = "batchUpdate", method = { RequestMethod.POST})
     @ResponseBody
     public JsonResult batchUpdate(@RequestBody List<SysDeparment> deparments) {
         JsonResult jr = new JsonResult();
         try {
             IResult res = sysDeparmentService.batchUpdate(deparments);
             jr.setResult(res);
             log.debug("修改部门成功【{}】", deparments);
         } catch (Exception ex) {
             log.error("修改部门出错", ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }
    
    /**
     * 去查看部门
     * 
     * @param id 部门页面表单对象唯一标识
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-11]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看部门详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
    @ResponseBody
    public JsonResult info(@PathVariable Long id) {
        JsonResult jr = new JsonResult();
        try {
            jr.setData(sysDeparmentService.getById(id));
        } catch (Exception ex) {
            log.error("查看部门出错", ex);
        }
        return jr;
        
    }
    
}
